
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>7、RANDOM · turf源码算法分析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="lijian">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="chapter8.html" />
    
    
    <link rel="prev" href="chapter6.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    INTRODUCE
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1.html">
            
                <a href="chapter1.html">
            
                    
                    1、MEASUREMENT
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter2.html">
            
                <a href="chapter2.html">
            
                    
                    2、COORDINATE MUTATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter3.html">
            
                <a href="chapter3.html">
            
                    
                    3、TRANSFORMATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter4.html">
            
                <a href="chapter4.html">
            
                    
                    4、FEATURE CONVERSION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter5.html">
            
                <a href="chapter5.html">
            
                    
                    5、MISC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="chapter6.html">
            
                <a href="chapter6.html">
            
                    
                    6、HELPER
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.8" data-path="chapter7.html">
            
                <a href="chapter7.html">
            
                    
                    7、RANDOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="chapter8.html">
            
                <a href="chapter8.html">
            
                    
                    8、DATA
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="chapter9.html">
            
                <a href="chapter9.html">
            
                    
                    9、INTERPOLATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="chapter10.html">
            
                <a href="chapter10.html">
            
                    
                    10、JOINS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="chapter11.html">
            
                <a href="chapter11.html">
            
                    
                    11、GRIDS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="chapter12.html">
            
                <a href="chapter12.html">
            
                    
                    12、CLASSIFICATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="chapter13.html">
            
                <a href="chapter13.html">
            
                    
                    13、AGGREGATION
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="chapter14.html">
            
                <a href="chapter14.html">
            
                    
                    14、META
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="chapter15.md">
            
                <span>
            
                    
                    15、ASSERTIONS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="chapter16.html">
            
                <a href="chapter16.html">
            
                    
                    16、BOOLEANS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="chapter17.md">
            
                <span>
            
                    
                    17、UNIT CONVERSION
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >7、RANDOM</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="randomposition">randomPosition</h1>
<p>&#x83B7;&#x53D6;bbox&#x5185;&#x7684;&#x968F;&#x673A;&#x70B9;</p>
<pre><code class="lang-javascript">[
    (<span class="hljs-built_in">Math</span>.random() * (bbox[<span class="hljs-number">2</span>] - bbox[<span class="hljs-number">0</span>])) + bbox[<span class="hljs-number">0</span>],
    (<span class="hljs-built_in">Math</span>.random() * (bbox[<span class="hljs-number">3</span>] - bbox[<span class="hljs-number">1</span>])) + bbox[<span class="hljs-number">1</span>]
];
</code></pre>
<h1 id="randompoint">randomPoint</h1>
<p>&#x5C06;randomPosition&#x4EA7;&#x751F;&#x7684;&#x70B9;&#x8F6C;&#x4E3A;Point</p>
<h1 id="randomlinestring">randomLineString</h1>
<p>&#x601D;&#x8DEF;&#xFF1A;</p>
<ol>
<li>&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x7684;&#x70B9;P1</li>
<li>&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x8DDD;&#x79BB;L&#x548C;&#x968F;&#x673A;&#x89D2;&#x5EA6;A&#xFF0C;&#x53EF;&#x53C8;P1&#x6C42;&#x51FA;&#x4E0B;&#x4E00;&#x4E2A;&#x70B9;P2&#xFF0C;&#x518D;&#x6839;&#x636E;P2&#x53EF;&#x6C42;&#x51FA;P3</li>
</ol>
<p>&#x4E0B;&#x9762;&#x7ED9;&#x51FA;&#x751F;&#x6210;&#x4E00;&#x6761;&#x968F;&#x673A;&#x7EBF;&#x7684;&#x7B97;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">/** &#x8BBE;&#x5B9A;&#x6298;&#x7EBF;&#x4E0A;&#x9762;&#x7EBF;&#x6BB5;&#x7684;&#x6700;&#x5927;&#x957F;&#x5EA6;&#x4E3A;max_length&#xFF0C;&#x6700;&#x5927;&#x7684;&#x504F;&#x8F6C;&#x89D2;&#x4E3A;max_rotation(&#x76F8;&#x90BB;&#x4E24;&#x6761;&#x7EBF;&#x6BB5;&#x7684;&#x5939;&#x89D2;,&#x9006;&#x65F6;&#x9488;&#x65B9;&#x5411;)*/</span>
<span class="hljs-comment">// &#x751F;&#x6210;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x7684;&#x70B9;</span>
<span class="hljs-keyword">const</span> startingPoint = randomPosition(bbox);
<span class="hljs-keyword">const</span> vertices = [startingPoint];
<span class="hljs-comment">// &#x5FAA;&#x73AF;&#x751F;&#x6210;&#x7EBF;&#x4E0A;&#x6240;&#x6709;&#x7684;&#x968F;&#x673A;&#x70B9;</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; n - <span class="hljs-number">1</span>; j++) {
    <span class="hljs-comment">// &#x5982;&#x679C;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x70B9;&#x5219;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x7684;&#x521D;&#x59CB;&#x89D2;&#x5EA6;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#x5219;&#x521D;&#x59CB;&#x89D2;&#x5EA6;&#x4E3A;&#x7EBF;&#x4E0A;&#x4E00;&#x6761;&#x7EBF;&#x6BB5;&#x7684;&#x89D2;&#x5EA6;</span>
    <span class="hljs-keyword">const</span> priorAngle = (j === <span class="hljs-number">0</span>) ?
          <span class="hljs-built_in">Math</span>.random() * <span class="hljs-number">2</span> * <span class="hljs-built_in">Math</span>.PI :
    <span class="hljs-built_in">Math</span>.tan(
        (vertices[j][<span class="hljs-number">1</span>] - vertices[j - <span class="hljs-number">1</span>][<span class="hljs-number">1</span>]) /
        (vertices[j][<span class="hljs-number">0</span>] - vertices[j - <span class="hljs-number">1</span>][<span class="hljs-number">0</span>]),
    );
    <span class="hljs-comment">// &#x521D;&#x59CB;&#x89D2;&#x5EA6;&#x52A0;&#x4E0A;&#x968F;&#x673A;&#x504F;&#x8F6C;&#x89D2;</span>
    <span class="hljs-keyword">const</span> angle = priorAngle + (<span class="hljs-built_in">Math</span>.random() - <span class="hljs-number">0.5</span>) * max_rotation * <span class="hljs-number">2</span>;
    <span class="hljs-comment">// &#x968F;&#x673A;&#x8DDD;&#x79BB;</span>
    <span class="hljs-keyword">const</span> distance = <span class="hljs-built_in">Math</span>.random() * max_length;
    <span class="hljs-comment">// &#x6839;&#x636E;&#x6B63;&#x4F59;&#x5F26;&#x51FD;&#x6570;&#x6C42;&#x51FA;&#x8BE5;&#x70B9;&#x5750;&#x6807;&#xFF0C;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x6B63;&#x4F59;&#x5F26;&#x51FD;&#x6570;&#x662F;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#x7684;&#xFF0C;&#x753B;&#x51FA;0-2&#x3C0;&#x7684;&#x56FE;&#x50CF;&#x53EF;&#x4EE5;&#x770B;&#x51FA;</span>
    vertices.push([
        vertices[j][<span class="hljs-number">0</span>] + distance * <span class="hljs-built_in">Math</span>.cos(angle),
        vertices[j][<span class="hljs-number">1</span>] + distance * <span class="hljs-built_in">Math</span>.sin(angle),
    ]);
}
</code></pre>
<h1 id="randompolygon">randomPolygon</h1>
<p>&#x751F;&#x6210;&#x968F;&#x673A;&#x7684;&#x51F8;&#x591A;&#x8FB9;&#x5F62;</p>
<p>&#x601D;&#x8DEF;&#xFF1A;&#x4EFB;&#x4F55;&#x7684;&#x51F8;&#x591A;&#x8FB9;&#x5F62;&#x7684;&#x5916;&#x89D2;&#x548C;&#x90FD;&#x662F;360&#xB0;&#xFF0C;&#x5916;&#x89D2;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x591A;&#x8FB9;&#x5F62;&#x4E0A;&#x76F8;&#x90BB;&#x7EBF;&#x6BB5;&#x7684;&#x504F;&#x8F6C;&#x89D2;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x51F8;n&#x53D8;&#x5F62;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x504F;&#x8F6C;n&#x6B21;&#x89D2;&#x5EA6;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x591A;&#x8FB9;&#x5F62;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">/** &#x751F;&#x6210;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x7684;&#x51F8;n&#x591A;&#x8FB9;&#x5F62;&#xFF0C;&#x591A;&#x8FB9;&#x5F62;&#x8FB9;&#x957F;&#x4E0D;&#x8D85;&#x8FC7;max_length
 */</span>
<span class="hljs-keyword">let</span> vertices: any[] = [];
<span class="hljs-comment">// &#x6BCF;&#x4E2A;&#x5916;&#x89D2;&#x7684;&#x504F;&#x8F6C;&#x56E0;&#x5B50;</span>
<span class="hljs-keyword">const</span> circleOffsets = <span class="hljs-built_in">Array</span>.apply(<span class="hljs-literal">null</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(n + <span class="hljs-number">1</span>)).map(<span class="hljs-built_in">Math</span>.random);

<span class="hljs-comment">// &#x7D2F;&#x52A0;&#x5916;&#x89D2;&#x7684;&#x504F;&#x8F6C;&#xFF0C;&#x65B9;&#x4FBF;&#x4E09;&#x89D2;&#x51FD;&#x6570;&#x8FD0;&#x7B97;</span>
circleOffsets.forEach((cur: any, index: number, arr: any[]) =&gt; {
    arr[index] = (index &gt; <span class="hljs-number">0</span>) ? cur + arr[index - <span class="hljs-number">1</span>] : cur;
});

circleOffsets.forEach((cur: any) =&gt; {
    cur = cur * <span class="hljs-number">2</span> * <span class="hljs-built_in">Math</span>.PI / circleOffsets[circleOffsets.length - <span class="hljs-number">1</span>];
    <span class="hljs-keyword">const</span> radialScaler = <span class="hljs-built_in">Math</span>.random();
    vertices.push([
        radialScaler * (max_length || <span class="hljs-number">10</span>) * <span class="hljs-built_in">Math</span>.sin(cur),
        radialScaler * (max_length || <span class="hljs-number">10</span>) * <span class="hljs-built_in">Math</span>.cos(cur),
    ]);
});
vertices[vertices.length - <span class="hljs-number">1</span>] = vertices[<span class="hljs-number">0</span>]; <span class="hljs-comment">// close the ring</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="chapter6.html" class="navigation navigation-prev " aria-label="Previous page: 6、HELPER">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="chapter8.html" class="navigation navigation-next " aria-label="Next page: 8、DATA">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"7、RANDOM","level":"1.8","depth":1,"next":{"title":"8、DATA","level":"1.9","depth":1,"path":"Chapter/chapter8.md","ref":"Chapter/chapter8.md","articles":[]},"previous":{"title":"6、HELPER","level":"1.7","depth":1,"path":"Chapter/chapter6.md","ref":"Chapter/chapter6.md","articles":[]},"dir":"ltr"},"config":{"plugins":["sharing","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["weibo","google","twitter","weibo","instapaper"]},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"lijian","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"turf源码算法分析","language":"zh-hans","gitbook":"3.2.3","description":"turf源码算法分析"},"file":{"path":"Chapter/chapter7.md","mtime":"2021-01-17T10:12:57.764Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-28T04:08:06.675Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

